package com.wangsan.study.web;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangsan.study.domain.ResourceEntity;
import com.wangsan.study.domain.ResourceEntityRepository;
import com.wangsan.study.domain.RoleEntity;
import com.wangsan.study.domain.RoleEntityRepository;
import com.wangsan.study.domain.UserEntity;
import com.wangsan.study.domain.UserEntityRepository;

/**
 * UserEntityController
 *
 * @author wangsan
 * @date 2016/12/28
 */
@Controller
@RequestMapping("/user")
public class UserEntityController {

    @Autowired
    private UserEntityRepository userEntityRepository;
    @Autowired
    private RoleEntityRepository roleEntityRepository;
    @Autowired
    private ResourceEntityRepository resourceEntityRepository;

    @RequestMapping("/list")
    public void list(Model model) {
        Iterable<UserEntity> userEntityIterable = userEntityRepository.findAll();
        model.addAttribute("userList", userEntityIterable);
    }

    @RequestMapping("/add")
    public void add(UserEntity user, Model model) {
        model.addAttribute("user", user);
    }

    @RequestMapping("/save")
    public String save(@Valid UserEntity user, BindingResult result, Model model) {
        userEntityRepository.save(user);
        return "redirect:/user/list";
    }

    @RequestMapping("/{id}")
    @ResponseBody
    public UserEntity view(@PathVariable("id") Long id) {
        return userEntityRepository.findOne(id);
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        userEntityRepository.delete(id);
        return "redirect:/user/list";
    }

    @RequestMapping("/actUserRole/{action}/{userId}/{roleId}")
    @ResponseBody
    public String userAndRole(@PathVariable("userId") Long userId,
                              @PathVariable("roleId") Long roleId,
                              @PathVariable("action") String action) {
        UserEntity userEntity = userEntityRepository.findOne(userId);
        RoleEntity roleEntity = roleEntityRepository.findOne(roleId);
        switch (action) {
            case "add":
                userEntity.getRoleEntitySet().add(roleEntity);
                break;
            case "remove":
                userEntity.getRoleEntitySet().remove(roleEntity);
                break;
            default:
                return "wrong action";
        }
        userEntityRepository.save(userEntity);
        return "success";
    }

    @RequestMapping("/actResRole/{action}/{resId}/{roleId}")
    @ResponseBody
    public String resourceAndRole(@PathVariable("resId") Long resId,
                                  @PathVariable("roleId") Long roleId,
                                  @PathVariable("action") String action) {
        ResourceEntity resourceEntity = resourceEntityRepository.findOne(resId);
        RoleEntity roleEntity = roleEntityRepository.findOne(roleId);
        switch (action) {
            case "add":
                resourceEntity.getRoleEntitySet().add(roleEntity);
                break;
            case "remove":
                resourceEntity.getRoleEntitySet().remove(roleEntity);
                break;
            default:
                return "wrong action";
        }
        resourceEntityRepository.save(resourceEntity);
        return "success";
    }
}
